<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
namespace App\Request;

use App\Constants\ErrorCode;
use App\Exception\BussinessException;
use Hyperf\Contract\ValidatorInterface;
use Hyperf\Validation\Request\FormRequest;

class DiaryRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     */
    public function rules(): array
    {
        return [
            'content' => 'required',
            'is_private' => 'required|in:0,1',
            'address' => 'filled',
        ];
    }

    public function messages(): array
    {
        return [
            'content.required' => '内容不能为空',
            'is_private.required' => '请选择是否私密',
            'is_private.in' => '是否私密参数值错误',
        ];
    }

    protected function failedValidation(ValidatorInterface $validator)
    {
        $msg = $validator->errors()->first();
        throw new BussinessException(ErrorCode::PARAM_ERROR, $msg);
    }
}
